package list;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ListAdapter extends BaseAdapter{

	public static final int ITEM_COUNT = 5;
	public static final int ITEM_VIEW_TYPE_ME = 0;
	public static final int ITEM_VIEW_TYPE_YOU = 1;
	public static final int ITEM_VIEW_TYPE_DATE = 2;
	public static final int ITEM_VIEW_TYPE_ME_PICTURE = 3;
	public static final int ITEM_VIEW_TYPE_YOU_PICTURE = 4;
	
	List<MyData> mList;
	Context mContext;
	
	public ListAdapter(Context context){
		mContext = context;
	}
	
	public ListAdapter(Context context, List<MyData> list){
		mContext = context;
		mList = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		MyData data = mList.get(position);
		
		
		if(data.isSend == true && data.isDate == false && data.isPicture == false){
			MyMsgSenderView view;
			if(convertView == null){
				view = new MyMsgSenderView(mContext);
			}else{
				view = (MyMsgSenderView) convertView;
			}
			view.setData(data);
						
			return view;
			
		}else if(data.isSend == false && data.isDate == false && data.isPicture == false){
			MyMsgView view;
			if(convertView == null){
				view = new MyMsgView(mContext);
			}else{
				view = (MyMsgView) convertView;
			}
			view.setData(data);
			
			
			return view;
			
		}else if(data.isDate == true && data.isPicture == false){
			DateView view;
			if(convertView == null){
				view = new DateView(mContext);
			}else{
				view = (DateView)convertView;
			}
			view.setData();
			
			
			return view;
		}else if(data.isSend == false && data.isDate == false && data.isPicture == true){
			MySendImageView view;
			if(convertView == null){
				view = new MySendImageView(mContext);
			}else{
				view = (MySendImageView)convertView;
			}
			view.setPicture(data.mPic);
			
			return view;
		}else if(data.isSend == true && data.isDate == false && data.isPicture == true){
			MyReceiveImageView view;
			if(convertView == null){
				view = new MyReceiveImageView(mContext);
			}else{
				view = (MyReceiveImageView)convertView;
			}
			view.setPicture(data.mPic);
			
			return view;
		}
		return null;

	}
	
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return ITEM_COUNT;
	}
	
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		MyData data = mList.get(position);
		
		if(data.isSend == true && data.isDate == false && data.isPicture == false){
			return ITEM_VIEW_TYPE_YOU;
		}else if(data.isSend == false && data.isDate == false && data.isPicture == false){
			return ITEM_VIEW_TYPE_ME;
		}else if(data.isDate == true && data.isPicture == false){
			return ITEM_VIEW_TYPE_DATE;
		}else if(data.isSend == true && data.isDate == false && data.isPicture == true){
			return ITEM_VIEW_TYPE_YOU_PICTURE;
		} else if((data.isSend == false && data.isDate == false && data.isPicture == true)){
			return ITEM_VIEW_TYPE_ME_PICTURE;
		}
		return 0;
	}

	public void add(MyData myData) {
		// TODO Auto-generated method stub
		mList.add(myData);
		notifyDataSetChanged();
	}

	public void addDate(MyData myData){
		mList.add(myData);
		notifyDataSetChanged();
	}

	public void addPicture(MyData myData) {
		// TODO Auto-generated method stub
		mList.add(myData);
		notifyDataSetChanged();
	}

}
